package com.csdj.questions.controller;


import com.csdj.questions.common.WebResult;
import com.csdj.questions.common.WebResultMsgCode;
import com.csdj.questions.entity.DiachronicRecord;
import com.csdj.questions.service.DiachronicRecordService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 历史记录表 前端控制器
 * </p>
 *
 * @author 刘宇
 * @since 2022-03-11
 */
@RestController
@RequestMapping("/diachronic-record")
public class DiachronicRecordController extends BaseController{

    @Resource
    private DiachronicRecordService diachronicRecordService;

    /**
     * 搜索框的历史记录查询，根据请求头id来查
     * @name 刘宇
     * @param request
     * @return
     */
    @GetMapping("/getInfoDiachronicRecord")
    public WebResult getInfoDiachronicRecord(HttpServletRequest request){
        int uid = getUid(request);
        DiachronicRecord diachronicRecord = diachronicRecordService.selectRecord(uid);
        if(diachronicRecord != null){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("diachronicRecord",diachronicRecord);
        }
        return null;
    }

    /**
     * 根据id 删除记录行  删除某一行搜索记录
     * @name 刘宇
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public WebResult deleteById(@RequestParam("id")int id){
        int deleteJg = diachronicRecordService.deleteIdDiachronicRecord(id);
        if(deleteJg > 0){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("deleteJg",deleteJg);
        }
        return null;
    }

    /**
     * 删除表的 所有记录行 删除所有搜索记录
     * @name 刘宇
     * @return
     */
    @GetMapping("/truncateDiachronicRecord")
    public WebResult truncateDiachronicRecord(){
        int truncateJg = diachronicRecordService.truncateDiachronicRecord();
        if(truncateJg == 0){ //truncate 返回0是成功 -1是失败
            return new WebResult(WebResultMsgCode.SUCCESS).putData("truncateJg",truncateJg);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }
}
